﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VSA.Presentation.Models;
using MVCControlsToolkit.Controller;
using MVCControlsToolkit.Linq;
using MVCControlsToolkit.ActionFilters;
using VSA.Business;
using VSA.DataAccess;
using VSA.Interfaces.Business;

namespace MvcApplication1.Controllers
{
    public class CustomerController : Controller
    {
        int numberOfPages;

        #region Customer List
        // **************************************
        // URL: /Customer/CustomerList
        // **************************************

        public ActionResult CustomerList()
        {
            CustomerListModel lm = new CustomerListModel();

            LoadCustomerList(ref lm, 1);

            return View(lm);
        }

        [HttpPost]
        public ActionResult CustomerList(CustomerListModel lm)
        {
            LoadCustomerList(ref lm, lm.Page);

            return View(lm);
        }

        public void CreatePages(out int numberOfPages, int pageNumber, ref List<Tracker<ToDoCustomerItem>> CurrentPage)
        {
            int numberOfItemsPerPage;

            //Query data
            ICustomers<IList<Customer>> custObject = (ICustomers<IList<Customer>>)BusinessFactory.CreateBusinessObject(BusinessEnumeration.Customer);
            IList<Customer> customerList = custObject.List(out numberOfPages,out numberOfItemsPerPage,pageNumber);

            //Prepare data for showing on pages
            CurrentPage = new List<Tracker<ToDoCustomerItem>>();
            
            for (int j = 0; j < numberOfItemsPerPage && j < customerList.Count; j++)
            {
                CurrentPage.Add(new Tracker<ToDoCustomerItem>(new ToDoCustomerItem()
                {
                    CustomerId = customerList.ElementAt(j).CustomerID,
                    CompanyName = customerList.ElementAt(j).CompanyName
                }));
                
            }

            TempData["CurrentPage"] = CurrentPage;
        }

        private Tracker<ToDoCustomerItem> FindTrackerToDo(string CustomerId)
        {
            List<Tracker<ToDoCustomerItem>> CurrentPage = TempData["CurrentPage"] as List<Tracker<ToDoCustomerItem>>;

            foreach (Tracker<ToDoCustomerItem> t in CurrentPage)
            {
                if (t.OldValue.CustomerId == CustomerId)
                    return t;
            }
            return null;
        }

        public ActionResult DisplayDetailToDo(string CustomerId)
        {
            if (CustomerId.Equals("")) return PartialView(new ToDoCustomerItem());

            Tracker<ToDoCustomerItem> t = FindTrackerToDo(CustomerId);
            if (t != null) return PartialView(t.OldValue);
            return PartialView(new ToDoCustomerItem());
        }

        private void LoadCustomerList(ref CustomerListModel lm, int pageNumber)
        {
            List<Tracker<ToDoCustomerItem>> CurrentPage = null;
            CreatePages(out numberOfPages, pageNumber, ref CurrentPage);

            //set page values to list model
            lm.Page = pageNumber;
            lm.TotalPages = numberOfPages;
            lm.ToDoCustomerList = CurrentPage;
        }
        #endregion

        #region Insert Update Delete Customer
        public ActionResult CreateDetailToDo()
        {
            return new MVCControlsToolkit.Controller.ClentValidationViewResult("../UserControl/Customer/DataGrid/CreateDetailToDo", new ToDoCustomerItem());
        }

        public ActionResult EditDetailToDo(string CustomerId)
        {
            if (TempData["CurrentPage"] == null)
            {
                List<Tracker<ToDoCustomerItem>> CurrentPage = null;
                CreatePages(out numberOfPages, 1, ref CurrentPage);
            }

            Tracker<ToDoCustomerItem> t = FindTrackerToDo(CustomerId);

            if (t != null) return new MVCControlsToolkit.Controller.ClentValidationViewResult("../UserControl/Customer/DataGrid/EditDetailToDo", t.Value);
            return new MVCControlsToolkit.Controller.ClentValidationViewResult("../UserControl/Customer/DataGrid/CreateDetailToDo", new ToDoCustomerItem());
        }


        [HttpPost]
        public ActionResult InsertEditDetailToDo(ToDoCustomerItem item)
        {
            if (Request.Form["mode"].Equals("Insert"))
            {
                //Insert customer data
                ICustomers<IList<Customer>> custObject = (ICustomers<IList<Customer>>)BusinessFactory.CreateBusinessObject(BusinessEnumeration.Customer);
                custObject.Insert(item.CustomerId, item.CompanyName);

                //Reload Page
                CustomerListModel lm = new CustomerListModel();
                LoadCustomerList(ref lm, 1);

                return new MVCControlsToolkit.Controller.ClentValidationViewResult("../UserControl/Customer/DataGrid/CreateDetailToDo", item);
            }
            else
            {
                List<Tracker<ToDoCustomerItem>> CurrentPage = TempData["CurrentPage"] as List<Tracker<ToDoCustomerItem>>;

                if (CurrentPage == null)
                {
                    CreatePages(out numberOfPages, 1, ref CurrentPage);
                }

                Tracker<ToDoCustomerItem> t = FindTrackerToDo(item.CustomerId);

                //Update customer data
                ICustomers<IList<Customer>> custObject = (ICustomers<IList<Customer>>)BusinessFactory.CreateBusinessObject(BusinessEnumeration.Customer);
                custObject.Update(t.Value.CustomerId, item.CompanyName);

                //Change data value on data grid
                t.OldValue = item;
                t.Value = item;

                return new MVCControlsToolkit.Controller.ClentValidationViewResult("../UserControl/Customer/DataGrid/EditDetailToDo", item);
            }
        }

        public ActionResult DeleteDetailToDo(string CustomerId)
        {
            //Delete customer data
            ICustomers<IList<Customer>> custObject = (ICustomers<IList<Customer>>)BusinessFactory.CreateBusinessObject(BusinessEnumeration.Customer);
            custObject.Delete(CustomerId);

            //Reload Page
            CustomerListModel lm = new CustomerListModel();
            LoadCustomerList(ref lm,1);

            return View("CustomerList", lm);
        }
        #endregion
    }
}
